package com.kaikeba.controller;

import com.kaikeba.bean.BootstrpExpress;
import com.kaikeba.bean.Express;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.ResultData;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.ExpressService;
import com.kaikeba.service.impl.ExpressServiceImpl;
import com.kaikeba.util.FormatUtil;
import com.kaikeba.util.JsonUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ExpressController {

    private ExpressService expressService = new ExpressServiceImpl();

    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp) {
        List<Map<String, Integer>> list = expressService.console();
        Message message = new Message();
        if (list != null) {
            message.setStatus(0);
            message.setData(list);
        } else {
            message.setStatus(-1);
            message.setResult("数据异常");
        }
        String s = JsonUtil.toJson(message);
        System.out.println("console-->" + s);
        return s;
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        resp.setContentType("text/html;charset=utf-8");
        ResultData<BootstrpExpress> data = new ResultData();
        int offset = Integer.parseInt(req.getParameter("offset"));
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        //数据
        List<Express> all = expressService.findAll(true, offset, pageNumber);
        List<BootstrpExpress> newAll = new ArrayList<>();
        for (Express e : all) {
            String inTime = FormatUtil.format(new Date(e.getInTime().getTime()));
            String outTime = e.getOutTime() == null ? "未出库" : FormatUtil.format(new Date(e.getOutTime().getTime()));
            String status = e.getStatus() == 0 ? "待取件" : "已取件";
            String code = e.getCode() == null ? "已取件" : e.getCode();
            BootstrpExpress express = new BootstrpExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), code, inTime, outTime, status, e.getSysPhone());
            newAll.add(express);
        }
        data.setRows(newAll);
        //数据总数
        List<Map<String, Integer>> console = expressService.console();
        Integer sum = console.get(0).get("data1_sum");
        data.setTotal(sum);
        String s = JsonUtil.toJson(data);
        System.out.println(s);
        return s;
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp) {
        resp.setContentType("text/html;charset=utf-8");
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        Express express = new Express(number, userName, userPhone, company, UserUtil.getPhone(req.getSession()));
        boolean result = expressService.insert(express);
        Message message = new Message();
        if (result) {
            message.setStatus(0);
            message.setResult("添加邮件成功");
        } else {
            message.setStatus(-1);
            message.setResult("添加邮件失败");
        }
        String s = JsonUtil.toJson(message);
        return s;
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
        resp.setContentType("text/html;charset=utf-8");
        String number = req.getParameter("number");
        Express byNumber = expressService.findByNumber(number);
        Message message = new Message();

        if (byNumber != null) {
            //查找成功
            message.setStatus(0);
            message.setResult("查找成功");
            message.setData(byNumber);
        } else {
            //查找失败
            message.setStatus(-1);
            message.setResult("抱歉，暂无此单号");
        }

        String s = JsonUtil.toJson(message);
        return s;
    }


    @ResponseBody("/express/updates.do")
    public String updates(HttpServletRequest req, HttpServletResponse resp) {
        resp.setContentType("text/html;charset=utf-8");

        int id = Integer.parseInt(req.getParameter("id"));
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        int status = Integer.parseInt(req.getParameter("status"));

        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setUserName(userName);
        newExpress.setUserPhone(userPhone);
        newExpress.setStatus(status);

        boolean update = expressService.update(id, newExpress);
        Message message = new Message();

        if (update){
            message.setStatus(0);
            message.setResult("修改成功");
        }else{
            message.setStatus(-1);
            message.setResult("修改失败");
        }
        String s = JsonUtil.toJson(message);
        return s;
    }


    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        resp.setContentType("text/html;charset=utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        Message message = new Message();
        boolean delete = expressService.delete(id);
        if (delete){
            message.setStatus(0);
            message.setResult("删除成功");
        }else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        String s = JsonUtil.toJson(message);
        return s;
    }
}
